package com.kt.delivery.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kt.delivery.domain.Order;
import com.kt.delivery.domain.User;
import com.kt.delivery.domain.dto.UserDto;
import com.kt.delivery.domain.dto.UserOrderByStatusDto;
import com.kt.delivery.domain.vo.OrderVo;
import com.kt.delivery.exception.ApiException;
import com.kt.delivery.mapper.OrderMapper;
import com.kt.delivery.mapper.UserMapper;
import com.kt.delivery.service.UserService;
import com.kt.delivery.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final OrderMapper orderMapper;

    private final LoginHelper loginHelper;

    /**
     * 修改用户信息
     * @param userDto
     * @return
     */
    @Override
    public int updateUser(UserDto userDto) {

        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(User::getUserName,userDto.getUserName());
        lambdaUpdateWrapper.set(User::getAddress,userDto.getAddress());
        lambdaUpdateWrapper.set(User::getPhone,userDto.getPhone());
        lambdaUpdateWrapper.set(User::getUpdateTime, LocalDateTime.now());
        lambdaUpdateWrapper.eq(User::getId,userDto.getId());
        int update = userMapper.update(lambdaUpdateWrapper);
        if (update<=0){
            throw new ApiException("修改失败","1001");
        }
        return update;
    }

    /**
     * 获取用户代取订单
     * @param userOrderByStatusDto 参数
     * @return 结果
     */
    @Override
    public List<OrderVo> getSubstituteOrderList(UserOrderByStatusDto userOrderByStatusDto) {

//        User user = userMapper.selectById(loginHelper.getUserId());

        // 获取用户订单
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (userOrderByStatusDto.getStatus() != null){
            orderLambdaQueryWrapper.eq(Order::getStatus,userOrderByStatusDto.getStatus());
        }

        // 代取件
        orderLambdaQueryWrapper.eq(Order::getPickStatus,1);

        // 用户信息
        orderLambdaQueryWrapper.eq(Order::getUserId,loginHelper.getUserId());

        List<Order> orders = orderMapper.selectList(orderLambdaQueryWrapper);

        List<OrderVo> orderVoList = orders.stream().map(order -> {

            List<String> listDeliceryImgs = JSON.parseArray(order.getDeliceryImgs(),String.class);

            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            orderVo.setDeliceryImgs(listDeliceryImgs);
            String phone = order.getPhone().substring(7, 11);
            orderVo.setPhone(phone);
            return orderVo;
        }).collect(Collectors.toList());

        return orderVoList;

    }

    /**
     * 获取用户代寄订单
     * @param userOrderByStatusDto 参数
     * @return 结果
     */
    @Override
    public List<OrderVo> getOthersOrderList(UserOrderByStatusDto userOrderByStatusDto) {
//        User user = userMapper.selectById(loginHelper.getUserId());

        // 获取用户订单
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (userOrderByStatusDto.getStatus() != null){
            orderLambdaQueryWrapper.eq(Order::getStatus,userOrderByStatusDto.getStatus());
        }

        // 代寄
        orderLambdaQueryWrapper.eq(Order::getPickStatus,0);

        // 用户信息
        orderLambdaQueryWrapper.eq(Order::getUserId,loginHelper.getUserId());

        List<Order> orders = orderMapper.selectList(orderLambdaQueryWrapper);

        List<OrderVo> orderVoList = orders.stream().map(order -> {

            List<String> listDeliceryImgs = JSON.parseArray(order.getDeliceryImgs(),String.class);

            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            orderVo.setDeliceryImgs(listDeliceryImgs);
            String phone = order.getPhone().substring(7, 11);
            orderVo.setPhone(phone);
            return orderVo;
        }).collect(Collectors.toList());

        return orderVoList;
    }
}
